package com.fps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fps.constants.SystemConstants;
import com.fps.domain.ResponseResult;
import com.fps.domain.dto.AddArticleDto;
import com.fps.domain.entity.ArticleTag;
import com.fps.domain.entity.Category;
import com.fps.domain.entity.Post;
import com.fps.domain.vo.HotPostVo;
import com.fps.domain.vo.PageVo;
import com.fps.domain.vo.PostDetailVo;
import com.fps.domain.vo.PostListVo;
import com.fps.mapper.PostMapper;
import com.fps.service.ArticleTagService;
import com.fps.service.CategoryService;
import com.fps.service.PostService;
import com.fps.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private  PostService postService;

    @Autowired
    private ArticleTagService articleTagService;

    @Override
    public ResponseResult hotArticleList() {
        //查询热门文章 封装成ResponseResult返回
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        //必须是正式文章
        queryWrapper.eq(Post::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //按照浏览量进行排序
        queryWrapper.orderByDesc(Post::getViewCount);
        //最多只查询10条
        Page<Post> page = new Page(1,10);
        page(page,queryWrapper);

        List<Post> posts = page.getRecords();
//        //bean拷贝
//        List<HotPostVo> postVos = new ArrayList<>();
//        for (Post post : posts) {
//            HotPostVo vo = new HotPostVo();
//            BeanUtils.copyProperties(post,vo);
//            postVos.add(vo);
//        }
        List<HotPostVo> vs = BeanCopyUtils.copyBeanList(posts, HotPostVo.class);

        return ResponseResult.okResult(vs);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        //查询条件
        LambdaQueryWrapper<Post> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 如果 有categoryId 就要 查询时要和传入的相同
        lambdaQueryWrapper.eq(Objects.nonNull(categoryId)&&categoryId>0 ,Post::getCategoryId,categoryId);
        // 状态是正式发布的
        lambdaQueryWrapper.eq(Post::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        // 对isTop进行降序
        lambdaQueryWrapper.orderByDesc(Post::getIsTop);

        //分页查询
        Page<Post> page = new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);

        List<Post> posts = page.getRecords();
        //postId去查询postName进行设置
        for (Post post : posts) {
            Category category = categoryService.getById(post.getCategoryId());
            post.setCategoryName(category.getName());
        }

        //封装查询结果
        List<PostListVo> postListVos = BeanCopyUtils.copyBeanList(page.getRecords(), PostListVo.class);

        PageVo pageVo = new PageVo(postListVos,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getPostDetail(Long id) {
        //根据id查询文章
        Post post = getById(id);
        //转换成VO
        PostDetailVo postDetailVo = BeanCopyUtils.copyBean(post, PostDetailVo.class);
        //根据分类id查询分类名
        Long categoryId = post.getCategoryId();
        Category category = categoryService.getById(categoryId);
        if(category!=null){
            postDetailVo.setCategoryName(category.getName());
        }
        UpdateWrapper<Post> updateWrapper=new UpdateWrapper<>();
        updateWrapper.setSql("`view_count`=`view_count`+1");
        postService.update(updateWrapper);
        //封装响应返回
        return ResponseResult.okResult(postDetailVo);
    }

    @Override
    @Transactional
    public ResponseResult add(AddArticleDto articleDto) {
        //添加 博客
        Post article = BeanCopyUtils.copyBean(articleDto, Post.class);
        save(article);


        List<ArticleTag> articleTags = articleDto.getTags().stream()
                .map(tagId -> new ArticleTag(article.getId(), tagId))
                .collect(Collectors.toList());

        //添加 博客和标签的关联
        articleTagService.saveBatch(articleTags);
        return ResponseResult.okResult();
    }
}
